<?php
	require_once 'Zend/Session/Namespace.php';

	require_once('Zend/Log.php');
    require_once('Zend/Log/Writer/Stream.php');
 	 
   
    
class UserController extends Zend_Controller_Action
{
	
	
    
    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {
        // action body
    }
    public function get_log(){
    	$logger = new Zend_Log(); 
    	$writer = new Zend_Log_Writer_Stream('../log/sample.log');
    	$logger->addWriter($writer);
    	return $logger;
    }
	public function loginAction()
    {
    	$logger = Zend_Registry::get('log'); 
    	echo "APPLICATION_PATH ".APPLICATION_PATH;    	
    	
    	//$data = ;
        //Zend_Debug::dump($data);
    	//$logger->log('sample message!', Zend_Debug::dump());
           
    	if ($this->_request->isPost()){
        	$username= $_POST['txt_username'];//$_POST['txt_username'];
        	$password= $_POST['txt_password'];
        	$logger -> info("name".$username);
			$logger -> info("pass".$password);
        	$flg = $this->check_user_exist($username,$password);
        	
        	if ($flg == 1){
        		require_once('Zend/Session.php');
    		 	$session = new Zend_Session_Namespace('identity');
    		 	$session->username = $username;
    		 	$logger -> info ("user naem".$session->username);
    		 	$this -> view -> assign('session_name',$session->username);
        	}
    	}
    	
    }
    public  function check_user_exist($name,$pass){
    	$flg = 0;
    	$logger = Zend_Registry::get('log'); 
    	$db = Zend_Registry::get('db');
    	   	   
		
		$logger -> info("name".$name);
		$logger -> info("pass".$pass);
    	$query =" select * from user where name = '$name' and password = '$pass'";
    	
    	$logger -> info($query);
    	if ($db->fetchOne($query)>0){
    		 $flg =1;
    		 
    		$logger -> info("ton tai");
    	}else{
    		$logger -> info("ko ton tai");
    		
    	}
    	return $flg;
    }

}

